const realWindowWidth = window.innerWidth > window.innerHeight
  ? window.innerHeight : window.innerWidth;

const realWindowHeight = window.innerWidth < window.innerHeight
  ? window.innerHeight : window.innerWidth;

export function rem2px(rem) {
  const rootFontSize = parseFloat(document.documentElement.style.fontSize);
  return rootFontSize * rem;
}

export function isAndroid() {
  return /(Android)/i.test(navigator.userAgent);
}

export function adjustRootFontSize() {
  const html = document.querySelector('html');
  html.style.fontSize = realWindowWidth / 7.5 + 'px';
}

/**
 * 自动滚动到指定元素位置
 * @param ele
 */
export function scrollToEle(ele) {
  const pageContentEle = document.querySelector('.page-content');
  const inputTop = ele.offsetTop;
  const currentScrollTop = pageContentEle.scrollTop;
  const expectScrollTop = currentScrollTop + inputTop - 44; // 减去导航条高度
  if (currentScrollTop < expectScrollTop) {
    pageContentEle.scrollTop = expectScrollTop;
  }
}

export function scrollToActivatedEle() {
  const ele = document.activeElement;
  const isKeyboardUp = window.innerHeight < realWindowHeight;
  const isInputEle = ['INPUT', 'TEXTAREA'].includes(ele.tagName);
  if (isKeyboardUp && isInputEle) {
    if (ele.scrollIntoViewIfNeeded) {
      ele.scrollIntoViewIfNeeded();
    } else {
      scrollToEle(ele);
    }
  }
}

/**
 * 随机返回指定长度字符串
 * @param len
 */
export function randomStr(len) {
  let str = Math.random().toString(36).substr(2);
  if (str.length >= len) {
    return str.substr(0, len);
  }
  str += randomStr(len - str.length);
  return str;
}

export function formatAmount(amt) {
  const price = parseFloat(amt);
  return '¥' + price.toFixed(2);
}

export function timeout(ms) {
  return new Promise((resolve, reject) => setTimeout(resolve, ms));
}

// 判断是否为特区，香港，澳门，台湾
export function isSpecialArea(provinceId) {
  const specialAreaGeoIds = ['CN-91', 'CN-92', 'CN-71'];
  return specialAreaGeoIds.includes(provinceId);
}

// 判断是否为正确的手机号码
export function isValidMobileNumber(mobile) {
  const str = mobile.replace(/\s/g, '');
  return /^\d{11}$/.test(str);
}

// 判断是否为正确的固话号码
export function isValidFixedNumber(fixedNumber) {
  const str = fixedNumber.replace(/\s/g, '');
  return /^\d(-|\d)+\d$/.test(str);
}

// 取小数部分
export function getDecimal(number) {
  return (+number).toFixed(2).split('.')[1];
}

// 去整数部分
export function getInteger(number) {
  return (+number).toFixed(2).split('.')[0];
}

// 格式化时间为指定样式 2017-09-10 10:10:10
export function formatDateTime(date) {
  const y = date.getFullYear();
  let m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  let d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  let h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  let minute = date.getMinutes();
  minute = minute < 10 ? ('0' + minute) : minute;
  let second = date.getSeconds();
  second = second < 10 ? ('0' + second) : second;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}

// 格式化时间为指定样式 2017.09.10
export function formatDate(date) {
  const y = date.getFullYear();
  let m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  let d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  return y + '.' + m + '.' + d;
}

/**
 * 数组里面包含对象去重方法
 * array 需要去重的数组
 * param 此参数相同才去重
 */
export function deDuplication(array, param) {
  const result = [];
  array.forEach((item) => {
    let index = -1;
    if (result.length) {
      index = result.findIndex((obj) => obj[param] === item[param]);
    }
    if (index < 0) {
      result.push(item);
    }
  });
  return result;
}
